package com.atguigu.lease.common.utils;

import com.atguigu.lease.common.constant.RedisConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;

/**
 * @ClassName JWTUtil
 * @Author heartFade
 * @Date 2025-05-04 15:59
 * @Version 1.0
 * @Description TODO
 **/
public class JWTUtil {

    private static final Key signKey= Keys.hmacShaKeyFor("44d7f67de095d9caa072ad31c54b271fd09a7976".getBytes());
    public static String createToken(Long id,String userName)
    {
      return  Jwts.builder().
                setSubject("lease-program").  //设置主题
                setExpiration(new Date(System.currentTimeMillis()+ RedisConstant.TOKEN_EXPIRE_TIME*100))
                .claim("id",id)
                .claim("userName",userName)
                .compressWith(CompressionCodecs.GZIP)
                .signWith(signKey)
                .compact();  //用来返回最终的token
    }

    public static Claims parseToken(String token)
    {
        return  Jwts.parserBuilder().setSigningKey(signKey).build().parseClaimsJws(token).getBody();
    }
}
